{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6c00e802",
   "metadata": {},
   "source": [
    "# DAY 26"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9014f315",
   "metadata": {},
   "source": [
    "到目前为止，我们已经使用了许多Python内置的函数（如 print(), len(), sum(), range()）以及各种库（如NumPy, Pandas）提供的函数。这些函数就像是别人为我们准备好的工具。今天，我们将学习如何创建自己的工具——自定义函数。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6998e3b",
   "metadata": {},
   "source": [
    "## 函数的定义"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e42d755",
   "metadata": {},
   "source": [
    "函数的基本写法如下所示：\n",
    "```python\n",
    "\n",
    "def function_name(parameter1, parameter2, ...):\n",
    "    \"\"\"\n",
    "    Docstring: 描述函数的功能、参数和返回值 (可选但强烈推荐)\n",
    "    \"\"\"\n",
    "    # 函数体: 实现功能的代码\n",
    "    # ...\n",
    "    return value # 可选，用于返回结果\n",
    "```\n",
    "\n",
    "- def: 关键字，表示开始定义一个函数。\n",
    "- function_name: 函数的名称，应遵循Python的命名约定（通常是小写字母和下划线，例如 calculate_area,用英文单词含义和下划线来作为函数名）。\n",
    "- parameter1, parameter2, ...: 函数的参数（也叫形参），是函数在被调用时接收的输入值。参数是可选的。\n",
    "- (): 参数列表必须放在圆括号中，即使没有参数，括号也不能省略。\n",
    "- : 冒号表示函数定义的头部结束，接下来是缩进的函数体。\n",
    "- Docstring (文档字符串): 位于函数定义第一行的多行字符串（通常用三引号 \"\"\"Docstring goes here\"\"\"）。用于解释函数的作用、参数、返回值等。可以通过 help(function_name) 或 function_name.__doc__ 查看。这个写法可选，为了后续维护和查看，建议加上这一段更加规范\n",
    "- 函数体 (Function Body): 缩进的代码块，包含实现函数功能的语句。\n",
    "- return value: return 语句用于从函数中返回一个值。如果函数没有 return 语句，或者 return 后面没有值，它会自动返回 None。一个函数可以有多个 return 语句（例如在不同的条件分支中）。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84f560f1",
   "metadata": {},
   "source": [
    "### 不带参数的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "6ff8ed19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "大家好！欢迎学习Python函数定义！\n"
     ]
    }
   ],
   "source": [
    "# 定义一个简单的问候函数\n",
    "def greet():\n",
    "    \"\"\"打印一句问候语。\"\"\"\n",
    "    message = \"大家好！欢迎学习Python函数定义！\"\n",
    "    print(message)\n",
    "\n",
    "greet()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe4e7d70",
   "metadata": {},
   "source": [
    "可以看到上述函数没有参数，也没有返回值，调用这个函数的时候运行函数体中的内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "5e6e70ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "打印一句问候语。\n"
     ]
    }
   ],
   "source": [
    "# 查看文档字符串,方便查看函数的使用，这个方法可以不掌握\n",
    "print(greet.__doc__)\n",
    "# 实际上，当你在py文件中，鼠标悬停在函数上按住ctrl即可点击函数跳转到其内部查看函数的定义"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40a92f6c",
   "metadata": {},
   "source": [
    "带上注释是函数定义的良好习惯，尤其在复杂的项目中，更方便维护和调试。可以让ai给你补上注释"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "499b8311",
   "metadata": {},
   "source": [
    "### 带参数的函数\n",
    "\n",
    "\n",
    "函数的参数我们有如下称呼：\n",
    "- Parameters (形参): 在函数定义中列出的变量名 (如 name, feature1, feature2)。\n",
    "- Arguments (实参): 在函数调用时传递给函数的实际值 (如 \"张三\", 10, 25)，也就是实际的数值（实参）传给了 形参（定义时候的变量）\n",
    "\n",
    "注意点： 定义的时候把函数的参数称之为形参，调用的时候把函数的参数称之为实参。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "475be9dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好, 张三! 很高兴认识你。\n"
     ]
    }
   ],
   "source": [
    "# 定义一个带一个参数的问候函数\n",
    "def greet_person(name):\n",
    "    \"\"\"根据给定的名字打印问候语。\n",
    "\n",
    "    Args:\n",
    "        name (str): 要问候的人的名字。\n",
    "    \"\"\"\n",
    "    message = f\"你好, {name}! 很高兴认识你。\"\n",
    "    print(message)\n",
    "\n",
    "greet_person(\"张三\")  # 输出: 你好, 张三! 很高兴认识你。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "b4dafeb7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 + 25 = 35\n"
     ]
    }
   ],
   "source": [
    "# 定义一个带多个参数的函数 (例如，在机器学习中计算两个特征的和)\n",
    "def add_features(feature1, feature2):\n",
    "    \"\"\"计算两个数值特征的和。\n",
    "\n",
    "    Args:\n",
    "        feature1 (float or int): 第一个特征值。\n",
    "        feature2 (float or int): 第二个特征值。\n",
    "    \"\"\"\n",
    "    total = feature1 + feature2\n",
    "    print(f\"{feature1} + {feature2} = {total}\")\n",
    "\n",
    "add_features(10, 25)       # 输出: 10 + 25 = 35\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "74d47f9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# add_features(5) \n",
    "# 这会报错，因为少了一个参数 TypeError"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91a87840",
   "metadata": {},
   "source": [
    "### 带返回值的函数\n",
    "\n",
    "函数不仅可以执行操作（如打印），还可以计算并返回一个结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "2f626dbd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定义一个计算和并返回结果的函数\n",
    "def calculate_sum(a, b):\n",
    "    \"\"\"计算两个数的和并返回结果。\n",
    "\n",
    "    Args:\n",
    "        a (float or int): 第一个数。\n",
    "        b (float or int): 第二个数。\n",
    "\n",
    "    Returns:\n",
    "        float or int: 两个数的和。\n",
    "    \"\"\"\n",
    "    result = a + b\n",
    "    return result\n",
    "    print(\"hhh\")\n",
    "\n",
    "calculate_sum(2, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd582dda",
   "metadata": {},
   "source": [
    "此时，注意到，print(\"hhh\")这个代码并没有被执行，因为函数在遇到return语句时，就会立即返回，而不会继续执行函数后面的代码。\n",
    "\n",
    "其次，如果没有return语句，或者return后面不带任何参数，那么函数也会返回None（不要把执行的操作理解为返回值）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "0aa84f24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据: [1, 2, 3, 4, 5]\n",
      "预处理后数据: [2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "# 函数可以返回多种类型的数据，包括列表、字典等\n",
    "# 例如，在数据预处理中，一个函数可能返回处理后的特征列表\n",
    "def preprocess_data(raw_data_points):\n",
    "    \"\"\"模拟数据预处理，例如将所有数据点乘以2。\n",
    "\n",
    "    Args:\n",
    "        raw_data_points (list): 原始数据点列表。\n",
    "\n",
    "    Returns:\n",
    "        list: 处理后的数据点列表。\n",
    "    \"\"\"\n",
    "    processed = []\n",
    "    for point in raw_data_points:\n",
    "        processed.append(point * 2) # 假设预处理是乘以2\n",
    "    return processed\n",
    "\n",
    "data = [1, 2, 3, 4, 5]\n",
    "processed_data = preprocess_data(data)\n",
    "\n",
    "print(f\"原始数据: {data}\")\n",
    "print(f\"预处理后数据: {processed_data}\") # 输出: [2, 4, 6, 8, 10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6319b89",
   "metadata": {},
   "source": [
    "## 变量作用域\n",
    "\n",
    "理解变量在何处可见和可访问非常重要。\n",
    "\n",
    "- 局部变量 (Local Variables): 在函数内部定义的变量，只在该函数内部有效。当函数执行完毕后，局部变量通常会被销毁。\n",
    "\n",
    "- 全局变量 (Global Variables): 在所有函数外部定义的变量，可以在程序的任何地方被访问（但在函数内部修改全局变量需要特殊声明，如 global 关键字，初学阶段可以先避免）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b6cdd6be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "--- 变量作用域示例 ---\n",
      "在函数内部，可以看到局部变量: '我是一个局部变量'\n",
      "在函数内部，也可以看到全局变量: '我是一个全局变量'\n",
      "\n",
      "在函数外部，可以看到全局变量: '我是一个全局变量'\n"
     ]
    }
   ],
   "source": [
    "print(\"\\n--- 变量作用域示例 ---\")\n",
    "global_var = \"我是一个全局变量\"\n",
    "\n",
    "def scope_test():\n",
    "    local_var = \"我是一个局部变量\"\n",
    "    print(f\"在函数内部，可以看到局部变量: '{local_var}'\")\n",
    "    print(f\"在函数内部，也可以看到全局变量: '{global_var}'\")\n",
    "    # global_var = \"尝试在函数内修改全局变量\" # 如果没有 global 声明，这会创建一个新的局部变量 global_var\n",
    "    # print(f\"在函数内部，修改后的 '全局' 变量: '{global_var}'\")\n",
    "\n",
    "scope_test()\n",
    "\n",
    "print(f\"\\n在函数外部，可以看到全局变量: '{global_var}'\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "3ff4344d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# print(f\"在函数外部，不能看到局部变量: {local_var}\") # 这会产生 NameError，因为 local_var 只在函数内存在"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f81c8a7f",
   "metadata": {},
   "source": [
    "## 函数的参数类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4167da84",
   "metadata": {},
   "source": [
    "在我们ctrl跳转到一些函数内部的时候，会发现写法相对我们日常定义的简单函数更加复杂，主要是参数形式比较丰富\n",
    "\n",
    "参数有以下类型：\n",
    "- 位置参数 (Positional Arguments): 调用时按顺序匹配。\n",
    "- 默认参数值 (Default Parameter Values): 定义函数时给参数指定默认值，调用时如果未提供该参数，则使用默认值。\n",
    "- 可变数量参数 (*args 和 **kwargs)：\n",
    "    - *args: 将多余的位置参数收集为一个元组。\n",
    "    - **kwargs: 将多余的关键字参数收集为一个字典。\n",
    "  \n",
    "可能你还听过关键字参数 (Keyword Arguments)这个说法，但是他并非是一种参数，而是一种传递参数的手段: 调用时通过 参数名=值 的形式指定，可以不按顺序。他可以传位置参数的值，也可以传默认参数的值，也可以传可变参数的值，也可以传关键字参数的值。为了可读性，更推荐对所有参数均采取关键字参数传递。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc1e9353",
   "metadata": {},
   "source": [
    "### 位置参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "497f024e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "我有一只 猫.\n",
      "我的 猫 的名字叫 咪咪.\n"
     ]
    }
   ],
   "source": [
    "def describe_pet(animal_type, pet_name):\n",
    "    \"\"\"显示宠物的信息。\"\"\"\n",
    "    print(f\"\\n我有一只 {animal_type}.\")\n",
    "    print(f\"我的 {animal_type} 的名字叫 {pet_name.title()}.\")\n",
    "\n",
    "describe_pet(\"猫\", \"咪咪\") # 使用关键字参数，顺序不重要"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e1a87bb",
   "metadata": {},
   "source": [
    "为了可读性，更推荐对所有参数采取关键词参数的写法\n",
    "```python\n",
    "假设一个复杂的绘图函数\n",
    "plot_data(data, x_col, y_col, \"blue\", \"-\", True, False, \"My Plot\", \"X-axis\", \"Y-axis\") # 不清晰\n",
    "\n",
    "使用关键字参数\n",
    "plot_data(data=my_data, x_column='time', y_column='value',\n",
    "          color='blue', linestyle='-', show_grid=True, use_log_scale=False,\n",
    "          title=\"My Awesome Plot\", xlabel=\"Time (s)\", ylabel=\"Value\") # 非常清晰\n",
    "```\n",
    "\n",
    "当一个函数有很多参数时，如果只用位置参数，调用者可能需要反复查看函数定义才能确定每个参数的含义。使用关键字参数，每个值的含义都通过其前面的参数名清晰地标示出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "3c1ab77d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "我有一只 猫.\n",
      "我的 猫 的名字叫 咪咪.\n",
      "\n",
      "我有一只 狗.\n",
      "我的 狗 的名字叫 旺财.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(animal_type=\"猫\", pet_name=\"咪咪\") # 使用关键字参数，顺序不重要\n",
    "describe_pet(pet_name=\"旺财\", animal_type=\"狗\") # 顺序改变，但结果正确"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c3aec50",
   "metadata": {},
   "source": [
    "### 默认参数\n",
    "\n",
    "注意点：带默认值的参数必须放在没有默认值的参数之后\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "78f4c1bf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我有一只 狗.\n",
      "我的 狗 的名字叫 小黑.\n",
      "我有一只 仓鼠.\n",
      "我的 仓鼠 的名字叫 雪球.\n"
     ]
    }
   ],
   "source": [
    "def describe_pet_default(pet_name, animal_type=\"狗\"): # animal_type 有默认值\n",
    "    \"\"\"显示宠物的信息，动物类型默认为狗。\"\"\"\n",
    "    print(f\"我有一只 {animal_type}.\")\n",
    "    print(f\"我的 {animal_type} 的名字叫 {pet_name.title()}.\")\n",
    "\n",
    "describe_pet_default(pet_name=\"小黑\") # animal_type 使用默认值 \"狗\"\n",
    "describe_pet_default(pet_name=\"雪球\", animal_type=\"仓鼠\") # 提供 animal_type，覆盖默认值\n",
    "# 注意：带默认值的参数必须放在没有默认值的参数之后"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "404e4a09",
   "metadata": {},
   "source": [
    "### *args (收集位置参数)\n",
    "\n",
    "*args: 将多余的位置参数收集为一个元组。\n",
    "\n",
    "当函数被调用时，Python 会先尝试用调用时提供的位置参数去填充函数定义中所有明确定义的、非关键字的形参 (也就是那些普通的，没有 * 或 ** 前缀的参数，包括有默认值的和没有默认值的)。\n",
    "如果在填充完所有这些明确定义的形参后，调用时还有剩余的位置参数，那么这些“多余的”位置参数就会被收集起来，形成一个元组 (tuple)，并赋值给 *args 指定的那个变量（通常就是 args）。\n",
    "如果调用时提供的位置参数数量正好等于或少于明确定义的形参数量（且满足了所有必需参数），那么 *args 就会是一个空元组 ()。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "6dd27424",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "制作一个 12 寸的比萨，配料如下:\n",
      "- 蘑菇\n",
      "\n",
      "制作一个 16 寸的比萨，配料如下:\n",
      "- 香肠\n",
      "- 青椒\n",
      "- 洋葱\n",
      "\n",
      "制作一个 9 寸的比萨，配料如下:\n",
      "- 原味 (无额外配料)\n"
     ]
    }
   ],
   "source": [
    "def make_pizza(size, *toppings):\n",
    "    \"\"\"概述要制作的比萨。\n",
    "    *toppings 会将所有额外的位臵参数收集到一个元组中。\n",
    "    \"\"\"\n",
    "    print(f\"\\n制作一个 {size} 寸的比萨，配料如下:\")\n",
    "    if toppings: # 只要toppings不为空元组，就会执行\n",
    "        for topping in toppings:\n",
    "            print(f\"- {topping}\")\n",
    "    else:\n",
    "        print(\"- 原味 (无额外配料)\")\n",
    "\n",
    "make_pizza(12, \"蘑菇\")\n",
    "make_pizza(16, \"香肠\", \"青椒\", \"洋葱\")\n",
    "make_pizza(9) # toppings 会是空元组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "464c7024",
   "metadata": {},
   "source": [
    "### **kwargs (收集关键字参数)\n",
    "\n",
    "**kwargs: 将多余的关键字参数收集为一个字典。\n",
    "\n",
    "当函数被调用时，Python 会先处理完所有的位置参数（包括填充明确定义的形参和收集到 *args 中）。\n",
    "然后，Python 会看调用时提供的关键字参数 (形如 name=value)。它会尝试用这些关键字参数去填充函数定义中所有与关键字同名的、明确定义的形参（这些形参可能之前没有被位置参数填充）。\n",
    "\n",
    "如果在填充完所有能通过名字匹配上的明确定义的形参后，调用时还有剩余的关键字参数（即这些关键字参数的名字在函数定义中没有对应的明确形参名），那么这些“多余的”关键字参数就会被收集起来，形成一个字典 (dictionary)，并赋值给 **kwargs 指定的那个变量（通常就是 kwargs）。\n",
    "\n",
    "如果调用时提供的所有关键字参数都能在函数定义中找到对应的明确形参名，那么 **kwargs 就会是一个空字典 {}。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "ec5ad45e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "用户信息: {'first_name': '爱因斯坦', 'last_name': '阿尔伯特', 'location': '普林斯顿', 'field': '物理学', 'hobby': '小提琴'}\n"
     ]
    }
   ],
   "source": [
    "def build_profile(first_name, last_name, **user_info):\n",
    "    \"\"\"创建一个字典，其中包含我们知道的有关用户的一切。\n",
    "    **user_info 会将所有额外的关键字参数收集到一个字典中。\n",
    "    \"\"\"\n",
    "    profile = {}\n",
    "    profile['first_name'] = first_name\n",
    "    profile['last_name'] = last_name\n",
    "    for key, value in user_info.items():\n",
    "        profile[key] = value\n",
    "    return profile\n",
    "\n",
    "user_profile = build_profile('爱因斯坦', '阿尔伯特',\n",
    "                             location='普林斯顿',\n",
    "                             field='物理学',\n",
    "                             hobby='小提琴')\n",
    "print(f\"\\n用户信息: {user_profile}\")\n",
    "# 输出: {'first_name': '爱因斯坦', 'last_name': '阿尔伯特', 'location': '普林斯顿', 'field': '物理学', 'hobby': '小提琴'}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb5c3a55",
   "metadata": {},
   "source": [
    "*args 和 **kwargs 的核心目的是让函数能够接收不定数量的参数，并以元组和字典的形式在函数内部进行处理。\n",
    "\n",
    "也就是说 当位置参数用完了 就自动变成*args，当关键词参数用完了 就自动变成**kwarges\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "0ec93a8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ID: 101\n",
      "Name: Alice\n",
      "Tags (*args): ('vip', 'new_user')\n",
      "Status: pending\n",
      "Details (**kwargs): {'location': 'USA', 'age': 30}\n",
      "--------------------\n",
      "ID: 102\n",
      "Name: Bob\n",
      "Tags (*args): ()\n",
      "Status: active\n",
      "Details (**kwargs): {'department': 'Sales'}\n",
      "--------------------\n",
      "ID: 103\n",
      "Name: Charlie\n",
      "Tags (*args): ('admin',)\n",
      "Status: pending\n",
      "Details (**kwargs): {}\n",
      "--------------------\n",
      "ID: 104\n",
      "Name: David\n",
      "Tags (*args): ()\n",
      "Status: pending\n",
      "Details (**kwargs): {'profession': 'Engineer'}\n",
      "--------------------\n"
     ]
    }
   ],
   "source": [
    "# 同时出现 *args 和 **kwargs，注意参数的传入顺序\n",
    "def process_data(id_num, name, *tags, status=\"pending\", **details): # 注意，这里的status 是仅关键字参数，必须通过关键词传值\n",
    "    print(f\"ID: {id_num}\")\n",
    "    print(f\"Name: {name}\")\n",
    "    print(f\"Tags (*args): {tags}\")\n",
    "    print(f\"Status: {status}\")          # status 是一个有默认值的普通关键字参数\n",
    "    print(f\"Details (**kwargs): {details}\")\n",
    "    print(\"-\" * 20)\n",
    "\n",
    "# 调用1:\n",
    "process_data(101, \"Alice\", \"vip\", \"new_user\", location=\"USA\", age=30)\n",
    "# ID: 101\n",
    "# Name: Alice\n",
    "# Tags (*args): ('vip', 'new_user')  <-- \"vip\", \"new_user\" 是多余的位置参数，被 *tags 收集\n",
    "# Status: pending                    <-- status 使用默认值，因为调用中没有 status=...\n",
    "# Details (**kwargs): {'location': 'USA', 'age': 30} <-- location 和 age 是多余的关键字参数，被 **details 收集\n",
    "# --------------------\n",
    "\n",
    "# 调用2:\n",
    "process_data(102, \"Bob\", status=\"active\", department=\"Sales\")\n",
    "# ID: 102\n",
    "# Name: Bob\n",
    "# Tags (*args): ()                   <-- 没有多余的位置参数\n",
    "# Status: active                     <-- status 被关键字参数 'active' 覆盖\n",
    "# Details (**kwargs): {'department': 'Sales'} <-- department 是多余的关键字参数\n",
    "# --------------------\n",
    "\n",
    "# 调用3:\n",
    "process_data(103, \"Charlie\", \"admin\") # 'admin' 会被 *tags 捕获\n",
    "# ID: 103\n",
    "# Name: Charlie\n",
    "# Tags (*args): ('admin',)\n",
    "# Status: pending\n",
    "# Details (**kwargs): {}\n",
    "# --------------------\n",
    "\n",
    "# 调用4: (演示关键字参数也可以用于定义中的位置参数)\n",
    "process_data(name=\"David\", id_num=104, profession=\"Engineer\")\n",
    "# ID: 104\n",
    "# Name: David\n",
    "# Tags (*args): ()\n",
    "# Status: pending\n",
    "# Details (**kwargs): {'profession': 'Engineer'}\n",
    "# --------------------\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "vs",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
